/**********************************
 * Assignment 5: Alien vs Predator
 * Date: 12/04/2011
 *
 * Alexander Miller: ahm82, 2605797
 * David Felty: djf242, 2828357
 * David Hau: dch229, 2586217
 * Hanna Atmer: ha237, 2666426
 **********************************/

package avp;

import java.util.HashSet;
import java.util.Set;

import ugraph.UGraphComponent;

// Represents an undirected edge in a graph
public class Edge implements UGraphComponent<Node> {
	
	// The set of both nodes at either end of this edge
	Set<Node> nodes = new HashSet<Node>();
	// Is the door open, closed, burned, or null (wall)?
	EdgeState state = EdgeState.OPEN;
	// Used for Ship.dijkstra
	int weight = 1;
	
	// Create a new Edge with no Nodes
	public Edge() {
	}
	
	// Create a new Edge connecting Nodes n1 and n2
	public Edge(Node n1, Node n2, EdgeState es) {
		super();
		nodes.add(n1);
		nodes.add(n2);
		state = es;
	}
	
	public Set<Node> getAdjacent() {
		return nodes;
	}
	
	public boolean equals(Object other) {
		try {
			Edge e = (Edge) other;
			Node a1 = (Node) nodes.toArray()[0];
			Node a2 = (Node) nodes.toArray()[1];
			Node b1 = (Node) e.nodes.toArray()[0];
			Node b2 = (Node) e.nodes.toArray()[1];
			return (a1.equals(b1) && a2.equals(b2)) || (a1.equals(b2) && a2.equals(b1));
		} catch (ClassCastException e) {
			return false;
		}
	}
	
	public int hashCode() {
		Node[] ns = nodes.toArray(new Node[2]);
		return ns[0].hashCode() * 13 + ns[1].hashCode() * 31;
	}
	
	public String toString() {
		return String.format("Edge (%s) (%s)", nodes.toArray()[0], nodes.toArray()[1]);
	}
	
	public Edge clone() {
		Edge e = new Edge();
		for (Node n : this.nodes) {
			e.nodes.add(n);
		}
		e.state = this.state;
		e.weight = this.weight;
		
		return e;
	}

}
